BASE = key_test
ELF_TARGET = $(BASE).elf
RAM_TARGET = $(BASE).ram
ROM_TARGET = $(BASE).rom
SRCS = key_test.c
OBJS = ${SRCS:.c=.o}

TC_PREFIX=arm-eabi-
CC=${TC_PREFIX}gcc
LD=${TC_PREFIX}ld
OBJDUMP=${TC_PREFIX}objdump
OBJCOPY=${TC_PREFIX}objcopy
READELF=${TC_PREFIX}readelf
NM=${TC_PREFIX}nm

CFLAGS= -Wall -g -Wstrict-prototypes -Wno-trigraphs -pipe -march=armv4t -mtune=arm920t
LDFLAGS= -N -p -X 

RAM_LDFLAG= -T${BASE}.ram.lds
ROM_LDFLAG= -T${BASE}.rom.lds

.PHONY: all clean extra-ram extra-rom

all:: ${RAM_TARGET} ${ROM_TARGET}

extra-ram: start.o ${OBJS}
	${LD} ${LDFLAGS} ${RAM_LDFLAG} -o ${ELF_TARGET} $^
	${OBJDUMP} -xS ${ELF_TARGET} > ${BASE}.asm
	${READELF} -a ${ELF_TARGET} > ${BASE}.r
	${NM} ${ELF_TARGET} > ${BASE}.n
	rm ${ELF_TARGET}

extra-rom: start.o ${OBJS}
	${LD} ${LDFLAGS} ${ROM_LDFLAG} -o ${ELF_TARGET} $^
	${OBJDUMP} -xS ${ELF_TARGET} > ${BASE}.asm
	${READELF} -a ${ELF_TARGET} > ${BASE}.r
	${NM} ${ELF_TARGET} > ${BASE}.n
	rm ${ELF_TARGET}

${RAM_TARGET}: start.o ${OBJS} 
	${LD} ${LDFLAGS} ${RAM_LDFLAG} -o ${ELF_TARGET} $^
	${OBJCOPY} -O binary ${ELF_TARGET} ${RAM_TARGET}
	rm ${ELF_TARGET}

${ROM_TARGET}: start.o ${OBJS} 
	${LD} ${LDFLAGS} ${ROM_LDFLAG} -o ${ELF_TARGET} $^
	${OBJCOPY} -O binary ${ELF_TARGET} ${ROM_TARGET}
	rm ${ELF_TARGET}

${OBJS}: %.o: %.c
	${CC} ${CFLAGS} -o $@ -c $< 

start.o: start.S
	${CC} ${CFLAGS} -o $@ -c $< 

clean:
	-rm -f *~ *.o ${ELF_TARGET} ${RAM_TARGET} ${ROM_TARGET} ${BASE}.asm ${BASE}.r ${BASE}.n

